Running the React Development Server – Detailed Notes
Topic: Running the React Development Server
Level: Beginner to Intermediate
Prerequisites: Basic knowledge of HTML, CSS, JavaScript, React, Node.js, npm, and React project structure.
1. Introduction to the React Development Server
A React development server is a local server that allows developers to run, view, test, and debug a React application during development. It serves the application's files to a web browser and helps developers see changes while working on the project.
In a typical React project created with Vite, the development server is started using the npm run dev command. Once the server starts, the terminal displays a local URL that you can open in your browser to view the application.
The development server is useful because it supports fast feedback during coding, module updates, and error reporting. Vite provides Hot Module Replacement (HMR), which can update changed modules without requiring a full page reload in many cases.
Example: When you edit App.jsx and save the file, the browser can update to show the new UI without manually restarting the server.
2. What Is a Development Server?
A development server is a tool that serves your application's files locally while you build and test your project. It is designed to support the development workflow rather than serve as the final production hosting environment.
For a typical Vite React application, the development server:
- Serves the HTML entry point and application modules.
- Processes supported JavaScript, JSX, and CSS files.
- Provides helpful error messages during development.
- Supports Hot Module Replacement (HMR).
- Allows you to test your application in a browser.
- Can use a configurable port and host address.
- Works with the project's installed dependencies and configuration.
Important: A development server is intended for local development and testing. A production application should be built and deployed using an appropriate hosting or deployment setup.
3. Tools Required to Run a React Development Server
3.1 Node.js
Node.js is a JavaScript runtime that allows JavaScript tools and development utilities to run outside the browser. React development tools such as Vite rely on Node.js.
Download Node.js from the official Node.js website.
3.2 npm
npm is a package manager commonly installed with Node.js. It helps developers install dependencies and run commands defined in the project's package.json file.
3.3 Code Editor
A code editor such as Visual Studio Code can help you create, edit, and manage React project files.
3.4 Web Browser
A modern browser such as Chrome, Firefox, Edge, or Safari allows you to view and test your React application.
3.5 Vite or Another Development Tool
Many modern React projects use Vite to provide the development server and build tooling. Other project setups may use different commands, so always check the project's package.json.
4. Check Whether Node.js and npm Are Installed
Before running a React project, check that Node.js and npm are available on your computer.
Open Command Prompt, PowerShell, or a terminal and execute:
node -v
npm -v
The commands display the installed versions when the tools are available.
Example output:
v22.x.x
10.x.x
The exact version numbers may differ depending on your installation.
If the commands are not recognized, install Node.js and reopen the terminal. For a new Vite project, check the current Vite documentation for the required Node.js version.
5. Creating a React Project with Vite
If you do not already have a React project, you can create one using Vite.
Step 1: Open a Terminal
Open your terminal or command prompt in the directory where you want to create your project.
Step 2: Run the Project Creation Command
npm create vite@latest my-react-app -- --template react
This command creates a new Vite project named my-react-app using the React template.
Step 3: Navigate to the Project Directory
cd my-react-app
This command changes the terminal's current directory to your new React project.
Step 4: Install Dependencies
npm install
This command installs the packages specified in the project's dependency configuration and creates or updates the local dependency installation.
Step 5: Start the Development Server
npm run dev
Vite starts its development server and prints a local URL in the terminal.
6. Running an Existing React Project
If you already have a React project, you usually do not need to create another one. Follow these steps to run the existing application.
- Open the project folder in your code editor.
- Open the integrated terminal or a separate terminal.
- Make sure the terminal is inside the folder containing
package.json.
- Install dependencies if they have not been installed.
- Start the development server using the script configured in
package.json.
For a typical Vite React project, use:
npm install
npm run dev
If dependencies are already installed and current, you can generally run only:
npm run dev
Important: If npm run dev reports that the script does not exist, open package.json and check the available scripts. Different project setups may use a different command.
7. Understanding the package.json File
The package.json file contains project metadata, dependencies, and scripts. npm uses scripts to run common project tasks.
A typical Vite React project's scripts section may look like this:
{
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
}
}
Let's understand the purpose of each script.
| Command |
Purpose |
npm run dev |
Starts the development server. |
npm run build |
Creates an optimized production build. |
npm run preview |
Locally previews the production build produced by Vite. |
Note: The scripts shown above are common Vite defaults. Your project may contain additional scripts or use different commands.
8. Understanding the Terminal Output
After running npm run dev, Vite usually displays information about the server and its local address.
Illustrative example:
VITE ready
Local: http://localhost:5173/
Network: use --host to expose
The exact wording, port number, and output format may vary.
8.1 Local URL
The local URL allows you to access the application from the same computer on which the development server is running.
8.2 Port Number
The port identifies the network endpoint used by the server. Vite commonly uses port 5173 by default, but it may select another available port if that port is already occupied.
8.3 Network Address
The network address option can allow other devices on your local network to access the development server when configured correctly. Exposing the server should be done carefully, especially on public or untrusted networks.
9. Opening the React Application in a Browser
Once the server starts successfully, open the local URL printed in the terminal.
For example:
http://localhost:5173/
The browser requests the application from the local development server. The HTML entry point loads the JavaScript entry file, which initializes React and renders the application's component tree.
If the starter React template is still in place, you may see the default Vite and React page. If you have edited App.jsx, the browser should display your custom interface.
Remember: Keep the terminal process running while you use the development server. Closing the terminal or stopping the process usually makes the local URL unavailable.
10. How the Development Server Works with React Files
A typical Vite React project contains several files that work together to display the application.
my-react-app/
├── index.html
├── package.json
├── vite.config.js
└── src/
├── main.jsx
├── App.jsx
├── App.css
└── index.css
Role of each file:
index.html provides the initial HTML document and loads the JavaScript entry point.
main.jsx initializes React and renders the App component into the root DOM element.
App.jsx defines the main application UI.
App.css contains styles used by the App component when imported.
index.css commonly contains global styles.
package.json lists dependencies and project scripts.
vite.config.js, when present, contains Vite configuration.
Application flow:
Run npm run dev
↓
Vite starts the development server
↓
Open the local URL in a browser
↓
Browser loads index.html
↓
main.jsx initializes React
↓
React renders App.jsx
↓
Application UI appears in the browser
11. Understanding Hot Module Replacement (HMR)
Hot Module Replacement (HMR) is a development feature that allows supported modules to update in the browser without requiring a full page reload in many cases.
Vite supports HMR to make the development process faster and more convenient.
Example
Suppose your App.jsx file contains:
function App() {
return (
<div>
<h1>Hello, React!</h1>
</div>
);
}
export default App;
Change the heading to:
<h1>Welcome to My React Project!</h1>
After you save the file, the browser may update the heading automatically.
Benefits of HMR:
- Reduces the need for manual browser refreshes.
- Speeds up UI development.
- Can preserve application state for supported updates.
- Helps developers test small changes quickly.
Note: Not every change can be applied without a reload. Some changes may trigger a full page refresh, depending on the module and application setup.
12. Restarting and Stopping the Development Server
12.1 Restarting the Server
Many source-code changes are picked up automatically. However, you may need to restart the server after changing certain configuration files or environment settings.
To restart the server:
- Go to the terminal where the server is running.
- Press
Ctrl + C to stop it.
- Run the development command again.
npm run dev
12.2 Stopping the Server
To stop the development server, press Ctrl + C in the terminal running the process. The server stops, and its local URL is no longer served by that process.
13. Changing the Development Server Port
If the default port is unavailable or you want to use a different port, you can specify one on the command line.
Example:
npm run dev -- --port 3000
This passes the --port 3000 option to Vite through the npm script.
You can also configure Vite to use a port in vite.config.js.
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
export default defineConfig({
plugins: [react()],
server: {
port: 3000
}
});
When you use a configuration file, ensure the required Vite React plugin is installed if your configuration imports it.
Important: If a port is occupied, Vite may choose another available port by default. If you want it to fail instead of switching ports, use the --strictPort option.
npm run dev -- --port 3000 --strictPort
14. Opening the Development Server on Other Devices
By default, a development server may listen only on a local interface. To access it from another device on the same network, you may need to configure the server to listen on a network interface.
Example:
npm run dev -- --host 0.0.0.0
Then use the computer's reachable local network IP address and the port shown by Vite on the other device.
Safety tips:
- Use this option only when you understand who can access the server.
- Keep devices on a trusted network.
- Do not expose development tools or sensitive project data to an untrusted network.
- Firewall settings may affect whether another device can connect.
15. Common Errors When Running the React Development Server
| Error or problem |
Possible cause |
Solution |
npm is not recognized |
Node.js/npm is not installed or is not available in the system PATH. |
Install Node.js, reopen the terminal, and check node -v and npm -v. |
| Missing dev script |
package.json does not define a dev script. |
Check the scripts section and use the command supported by your project. |
vite is not found |
Dependencies have not been installed or Vite is not configured as a project dependency. |
Run npm install and inspect the project dependencies. |
| Port already in use |
Another process is using the requested port. |
Stop the other process or start the server on another available port. |
| Blank browser page |
The application may have a runtime error, a broken import, or a missing root element. |
Check the browser console, terminal output, and React entry-point files. |
| Failed to resolve import |
An imported file path or package name is incorrect. |
Verify the filename, path, capitalization, and dependency installation. |
| Changes do not appear |
The file may not have been saved, HMR may have failed, or the wrong server may be open. |
Save the file, inspect terminal output, verify the URL, and restart the server if necessary. |
| Unsupported Node.js version |
The installed Node.js version does not meet the tool's requirements. |
Check the current Vite requirements and install a compatible Node.js version. |
| Cannot connect to localhost |
The server is not running, or the URL/port is incorrect. |
Restart the server and use the exact local URL printed in the terminal. |
| Network device cannot connect |
The server is listening only locally, or network/firewall rules block access. |
Check the host setting, local IP address, port, and firewall rules. |
16. Troubleshooting Step by Step
If the React application does not start, follow this checklist.
- Check Node.js: Run
node -v.
- Check npm: Run
npm -v.
- Check the current directory: Make sure the terminal is in the project folder containing
package.json.
- Install dependencies: Run
npm install if required.
- Check scripts: Open
package.json and confirm the available commands.
- Start the server: Run
npm run dev for a typical Vite project.
- Use the correct URL: Open the URL shown in the terminal.
- Check terminal errors: Read any errors printed while the server starts or processes source files.
- Check browser DevTools: Open the browser console and inspect runtime errors.
- Restart if needed: Stop the server with
Ctrl + C and start it again.
17. Development Server vs Production Build
A development server and a production build serve different purposes.
| Feature |
Development server |
Production build |
| Purpose |
Developing, testing, and debugging the application. |
Preparing optimized files for deployment. |
| Typical Vite command |
npm run dev |
npm run build |
| Output |
Serves source modules and development features. |
Generates optimized static assets, commonly in dist. |
| Hot Module Replacement |
Available during development. |
Not used as a development feature in the deployed build. |
| Typical usage |
Local development. |
Deployment to a hosting platform or web server. |
To create a production build in a Vite project, run:
npm run build
To preview the built output locally, run:
npm run preview
Important: Vite's preview command is intended to test the production build locally. It is not a replacement for a properly configured production hosting service.
18. Practical Exercise: Run and Update a React Application
Objective: Start a React development server and verify that code changes appear in the browser.
Step 1: Open the Project
Open your Vite React project in a code editor and launch its terminal.
Step 2: Install Dependencies
npm install
Step 3: Start the Development Server
npm run dev
Step 4: Open the Local URL
Copy the local URL from the terminal and open it in your browser.
Step 5: Update App.jsx
function App() {
return (
<div>
<h1>React Development Server</h1>
<p>My React application is running successfully.</p>
<button onClick={() => alert('React is working!')}>
Test Application
</button>
</div>
);
}
export default App;
Step 6: Save and Test
Save App.jsx. The browser should update to display the heading, paragraph, and button. Click the button to test its event handler.
Step 7: Stop the Server
Return to the terminal and press Ctrl + C.
Expected learning outcome: You will know how to start, access, update, test, and stop a React development server.
19. Best Practices for Running the React Development Server
- Confirm that Node.js and npm meet the project's requirements.
- Run commands from the directory containing the correct
package.json.
- Install dependencies before starting a newly downloaded or cloned project.
- Read the terminal output instead of assuming the server uses a particular port.
- Open the exact URL displayed by the development server.
- Use the browser console and terminal to identify errors.
- Keep development dependencies and project configuration consistent.
- Use the development server for coding and testing, not as the final production hosting solution.
- Stop the server when you no longer need it.
- Be cautious when exposing the development server to other devices or networks.
20. Important Interview Questions and Answers
Q1. What is a React development server?
Answer: It is a local server that serves a React application during development and helps developers test, debug, and preview code changes in a browser.
Q2. Which command starts a Vite React development server?
Answer: The standard command is npm run dev, provided that the project's package.json defines a dev script.
Q3. What is Vite?
Answer: Vite is a frontend build tool that provides a development server and build process for modern web projects, including React applications.
Q4. What is npm?
Answer: npm is a package manager commonly used to install project dependencies and run scripts defined in package.json.
Q5. What does npm install do?
Answer: It installs the project's dependencies based on its package configuration and lockfile, when available.
Q6. What is localhost?
Answer: localhost refers to the local computer. A development server can use a localhost URL so the application can be accessed from the same machine.
Q7. What is a port number?
Answer: A port identifies the network endpoint through which a server accepts connections. Vite commonly starts on port 5173, but the actual port can vary.
Q8. What is Hot Module Replacement?
Answer: HMR updates supported modules in the browser during development without requiring a full page reload in many cases.
Q9. How do you stop the development server?
Answer: Press Ctrl + C in the terminal where the server is running.
Q10. What should you do if the port is already in use?
Answer: Stop the process using the port or select another available port using an option such as npm run dev -- --port 3000.
Q11. What is the difference between npm run dev and npm run build?
Answer: npm run dev starts the development server, while npm run build generates an optimized production build in a typical Vite project.
Q12. Why might npm run dev fail?
Answer: Common reasons include missing dependencies, an incorrect project directory, a missing script, an incompatible Node.js version, or a configuration error.
Q13. Do you need to restart the server every time you change App.jsx?
Answer: Usually not. Vite supports automatic updates and HMR for many source changes. Certain configuration changes may require restarting the server.
Q14. Is the development server suitable for production hosting?
Answer: No. It is intended for development. Build the application and deploy the resulting assets using an appropriate production hosting setup.
21. Quick Revision Checklist
- Understand the purpose of a React development server.
- Know how to check Node.js and npm versions.
- Understand how to create a React project with Vite.
- Know how to install dependencies using
npm install.
- Know how to start the server using
npm run dev.
- Understand how to open the local URL in a browser.
- Know the purpose of
package.json scripts.
- Understand Hot Module Replacement (HMR).
- Know how to change the port and stop the server.
- Be able to troubleshoot common startup errors.
- Understand the difference between development and production builds.
22. Conclusion
Running the React development server is an essential part of React development. It allows you to open your application locally, test your components, identify errors, and view code changes quickly. In a typical Vite React project, the main steps are to install dependencies, run npm run dev, open the displayed local URL, and keep the server running while you work.
For structured learning, visit the JustAcademy React JS Training Course.
To request more course information or a demonstration, visit Register for React JS Course Demo.
23. Additional Learning Resources